<?php
$str = file_get_contents("css/style.css");

preg_match_all("/http:\/\/(.+)([^\/]+\.[(gif)|(png)|(jpg)][a-z]{2})/Uis",$str,$out);

$ch = curl_init();
$timeout = 300; // set to zero for no timeout
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//跟随页面的跳转
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$name = $out[2];

foreach ($out[0] as $key=>$value)
{
	$fh = fopen($name[$key],"w");
	curl_setopt($ch, CURLOPT_URL, $value);
	$content = curl_exec($ch);
	fwrite($fh, $content);
	fclose($fh);
}

var_dump($out);exit;
$i = 0;
while (!feof($fp1)) {
   $modle = fgetcsv($fp1, 1000, "\t");
   $i++;
   if ($i == 2)
   	break;
}

$fp2 = fopen("liankuwa.csv","r");

while (!feof($fp2)) {
	$modle2 = fgetcsv($fp2, 1000, "\t");
	//var_dump($modle);exit;
	$arr['title'] = $modle2[0];
	$arr['price'] = $modle2[7];
	$arr['intro'] = $modle2[24];
	//下载intro中的图片
	$pattern = "/src=\"(.*)\"/Uims";
	preg_match_all($pattern, $arr['intro'], $match);
	foreach ($match[1] as $key=>$val)
	{
		//获取图片
		$path = explode('/', $val);
		$filename = $path[count($path)-1];
		$fh = fopen("img/".$filename, 'w');
		curl_setopt($ch, CURLOPT_URL, $val);
		$content = curl_exec($ch);
		fwrite($fh, $content);
		fclose($fh);
	}
	//将简介中的图片替换成本地
	$search = array(
		"http://v6.freep.cn/",
		"http://v7.freep.cn/",
	);
	$replace = array(
		"D:/www/mytest/langsha/img/",
		"D:/www/mytest/langsha/img/",
	);
	$arr['intro'] = str_replace($search, $replace, $arr['intro']);
	$modle[0] = $arr['title'];
	$modle[7] = $arr['price'];
	$modle[24] = $arr['intro'];
	//var_dump($modle);exit;
	fputcsv($fp1, $modle, "\t");
}
echo "ok";
fclose($fp2);
fclose($fp1);

exit;
$sale = $modle[37];
$fp3 = fopen("xuezi.csv","a");
foreach ($urls as $k=>$v)
{
	$url = "http://maryxiu.q88k.net/shop_gkk3/products.aspx?sku=$v&shbid=21399";
	curl_setopt($ch, CURLOPT_URL, $url);
	
	$handles = curl_exec($ch);
	
	if (strpos($handles, '服务器错误') !== false)
	{
		echo 1;
		break;
	}
	$title_pattern = "/<span class=\"style24\"> <span id=\"Label1\">([^<]*)<\/span>/mis";	
	preg_match($title_pattern, $handles, $matchs);
	$arr['title'] = $matchs[1];
	$price_pattern = "/<span id=\"Label5\">[^\d]*([\d]*)[^<]*<\/span>/mis";
	preg_match($price_pattern, $handles, $matchs2);
	//var_dump($matchs2);
	$arr['price'] = $matchs2[1]*2;
	
	//简介
	$intro_pattern = "/<span id=\"Label7\">(.*)<\/span>/Umis";
	preg_match($intro_pattern, $handles, $matchs3);
	$arr['intro'] = $matchs3[1];
	var_dump($arr);exit;
	//将图片下到本地,并去水印
	$image_pattern = "/<img (.?*) id=\"img_tp\"/Umis";
	preg_match($image_pattern, $handles, $matchs4);
	$arr['image'] = $matchs4[1];
	/*
	$modle[0] = $arr['title'];
	$modle[7] = $arr['price'];
	$modle[24] = $arr['intro'];
	$modle[37] = str_replace('1.000000', $arr['price'], $sale);
	
	fputcsv($fp3, $modle, "\t");*/
}
fclose($fp3);
echo "ok";
?> 